package by.grsu.mikhailava.page;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.mikhailava.app.TestWebApplication;
import by.grsu.mikhailava.entity.User;
import by.grsu.mikhailava.entity.UserRole;
import by.grsu.mikhailava.dao.UserDao;


public class Registration extends WebPage{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2688574331569319118L;
	
	@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
	public Registration() {
		super();
		final User user= new User();
		final Form<User> form = new Form<User>("form",new CompoundPropertyModel<User>(user));
		add(form);

		form.add(new TextField<String>("firstName"));
		form.add(new TextField<String>("surName"));
		form.add(new TextField<String>("login"));
		form.add(new TextField<String>("password"));
		final List role = Arrays.asList(UserRole.tutor, UserRole.student);
		ListChoice listChoice = new ListChoice("role", role);
		listChoice.setMaxRows(2);
		form.add(listChoice);
		
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				final UserDao userDao = new UserDao(
						TestWebApplication.DATA_XML_FOLDER);
				if (user.getId() == null) {
					userDao.saveNew(user);
				} else {
					userDao.update(user);
				}
				setResponsePage(MyFirstWebPage.class);
			}
		});

		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(MyFirstWebPage.class);
			}
		});
	}

}
